<?php
class Gi_Xls {
  private $_content = null;
  
  private function _clear() {
    $this->_content = null;
  }
  
  function get_result() {
    return 
      '<html xmlns:o="urn:schemas-microsoft-com:office:office"
       xmlns:x="urn:schemas-microsoft-com:office:excel"
       xmlns="http://www.w3.org/TR/REC-html40">
       <head>

       <style>
          td{
            white-space:nowrap;          
          }
          .xl26
            {mso-style-parent:style0;
            mso-number-format:"m\/d\/yyyy\ h\:mm\:ss\ AM\/PM";
            white-space:normal;}
       </style>
       <!--[if gte mso 9]><xml>
        <x:ExcelWorkbook>
         <x:ExcelWorksheets>
          <x:ExcelWorksheet>
           <x:Name>Sheet1</x:Name>
           <x:WorksheetOptions>
            <x:Selected/>
           </x:WorksheetOptions>
          </x:ExcelWorksheet>
         </x:ExcelWorksheets>
        </x:ExcelWorkbook>
       </xml><![endif]-->
       </head>
       <body>
       <TABLE>
           '.$this->content.'
       </TABLE></html>';    
  }  
  
  public function save_to_file($filename) {
    $fp = @fopen($filename,'w+');
    if ($fp) {
      fwrite($fp,$this->get_result());
      fclose($fp);
      return true;
    }
    else {
      return false;
    }
  }
  
  public function output_to_browser($filename) {
    
    $tmp = $this->get_result();
    
    header("Pragma: public");
    header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);      
    header("Content-Transfer-Encoding: binary");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    header("Content-Length: ".strlen($tmp));

    echo $tmp;
    exit;  
  }
  
  public function add_record(&$row,$params=false) {
    foreach($row as $key=>$value) {
      $rec_line .= '<td>'.$value.'</td>';   //  nowrap    nowrap="nowrap"
    }
    $this->content .= '<tr '.$params.'>'.$rec_line.'</tr>';
  }
}